package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.TokenCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.util.json.JsonConversion;
import com.vanvalt.payhui.util.qiniu.QiniuUtil;

/**
 * 七牛Token处理接口
 * @author Xiaoyang
 *
 */
public class TokenProcess extends BaseProcess {
	/**
	 *日志.
	 */
	@SuppressWarnings("unused")
	private static Logger 		logger = Logger.getLogger(TokenProcess.class);
	private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();
	
	private UserService userService;
	
	/**
	 * 构造方法.
	 */	
	public TokenProcess() {
		super();
	}
	
	public TokenProcess(UserService userService) {
		super();
		this.userService = userService;
	}
	
	/**
	 * 生产令牌.
	 * @return
	 * @throws AuthException
	 * @throws JSONException
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public ResponseCommand generateToken(RequestCommand requestCommand,ResponseCommand responseCommand) throws JSONException, JsonParseException, JsonMappingException, IOException {
		
		Map<String, Object> resultMap = Maps.newHashMap();
		
		// 解析json为Token对象
		TokenCommand tokenCommand = new TokenCommand(requestCommand.getObject()
				.toString());
		
		Map<String, Object> map = tokenCommand.getMap();
		String uid = (String) map.get("uid");
		
		if(map.isEmpty() && StringUtils.isBlank(uid)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		String accessKey = Global.getConfig("qiniu.config.accesskey");
        		String secretKey = Global.getConfig("qiniu.config.secretkey");
        		
        		try {
        			
        			String token = new QiniuUtil(accessKey, secretKey).getUpToken();
        			resultMap.put("token", token);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_QINIU_TOKEN, responseCommand);
				}
        		
        		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
         				JsonConversion.writeMapJSON(resultMap), responseCommand);
        	}
        }
		
		/*Token token = tokenCommand.getToken();
		if (null == token) {
			responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_FALSE);
			responseCommand.setCode(ErrorCode.ERR_CODE_PRECONDITION_FAILED);
		} else {
			// TODO 需要判断生成的令牌类型
			token.setToken(new Uptoken(webConfig).generateUptoken());
			responseCommand.setObject(JsonConversion.writeEntityJSON(token));
			responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_TRUE);
			responseCommand.setCode(ErrorCode.ERR_CODE_SUCCESS);
		}		*/
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
}
